<?php

namespace App\Models\Backend\System;

use App\Models\Mobile\MobileStudent;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Student extends Authenticatable
{
    protected $table = 'students';

    protected $guarded = [];

    protected $primaryKey = 'cid';

    protected $keyType = 'string';

    protected $appends = ['stage_text','birthday'];

    private $stage_diccode = 'STUDY_SECTION';

    public $incrementing = false;

    public $timestamps = false;

    /**
     * 获取与学生绑定的手机号
     */
    public function mobile()
    {
        $mobileStudent = MobileStudent::where('cid', $this->cid)->first();
        if($mobileStudent){
            return $mobileStudent->mobile;
        }else{
            return '';
        }
    }

    public function getStageTextAttribute()
    {
        $val = $this->attributes['stage'] ?? '';
        if ($val){
            $arr = explode(',',$val);
            $res =  Dicitem::where('diccode',$this->stage_diccode)->whereIn('itemcode',$arr)->get();
            $returnData = [];
            foreach ($res as $re) {
                $returnData[] = $re->itemname;
            }
            return implode(',',$returnData);
        }
        return $val;
    }
    public function getBirthdayAttribute()
    {
        $birthday = $this->attributes['birthday'] ?? '';
        if ($birthday) return $birthday;
        $val = $this->attributes['cid'] ?? '';
        if ($val){
            $str = substr($val,6,8);
            return date('Y-m-d',strtotime($str));
        }
        return $val;

    }
    public function getGenderAttribute()
    {

        $gender = $this->attributes['gender'] ?? '';
        if ($gender) return $gender;
        $val = $this->attributes['cid'] ?? '';
        if ($val){
            $str = substr($val,-2,1);
            if ($str % 2 == 0){
                return '女';
            }else{
                return '男';
            }

        }
        return $val;

    }
}
